<template>
  <div class="test-page">
    <div class="container">
      <h2>认证状态测试</h2>
      
      <div class="status-section">
        <h3>用户状态</h3>
        <p><strong>认证状态:</strong> {{ userStore.isAuthenticated ? '已认证' : '未认证' }}</p>
        <p><strong>登录状态:</strong> {{ userStore.isLoggedIn ? '已登录' : '未登录' }}</p>
        <p><strong>用户信息:</strong> {{ userStore.user?.nickname || '无' }}</p>
        <p><strong>Token:</strong> {{ userStore.token ? '存在' : '不存在' }}</p>
      </div>
      
      <div class="action-section">
        <h3>操作测试</h3>
        <div class="action-buttons">
          <el-button type="primary" @click="checkAuth">检查认证状态</el-button>
          <el-button type="warning" @click="clearAuth">清除认证信息</el-button>
          <el-button type="danger" @click="logout">退出登录</el-button>
        </div>
      </div>
      
      <div class="test-section">
        <h3>功能测试</h3>
        <div class="test-buttons">
          <el-button type="primary" @click="testOrders">测试订单页面</el-button>
          <el-button type="success" @click="testMessages">测试消息页面</el-button>
          <el-button type="warning" @click="testProfile">测试个人中心</el-button>
          <el-button type="info" @click="testSettings">测试设置页面</el-button>
        </div>
      </div>
      
      <div class="debug-section">
        <h3>调试信息</h3>
        <pre>{{ debugInfo }}</pre>
      </div>
      
      <div class="localStorage-section">
        <h3>本地存储检查</h3>
        <p><strong>Token:</strong> {{ localStorageToken || '无' }}</p>
        <p><strong>用户信息:</strong> {{ localStorageUserInfo || '无' }}</p>
        <div class="action-buttons">
          <el-button size="small" @click="refreshLocalStorage">刷新本地存储</el-button>
          <el-button size="small" type="danger" @click="clearLocalStorage">清空本地存储</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'

const router = useRouter()
const userStore = useUserStore()

const localStorageToken = ref(localStorage.getItem('token'))
const localStorageUserInfo = ref(localStorage.getItem('userInfo'))

const debugInfo = computed(() => ({
  isAuthenticated: userStore.isAuthenticated,
  isLoggedIn: userStore.isLoggedIn,
  user: userStore.user,
  token: userStore.token ? '存在' : '不存在',
  localStorage: {
    token: localStorage.getItem('token'),
    userInfo: localStorage.getItem('userInfo')
  }
}))

const checkAuth = async () => {
  const result = await userStore.checkAuth()
  ElMessage.success(`认证检查结果: ${result ? '成功' : '失败'}`)
  refreshLocalStorage()
}

const clearAuth = () => {
  userStore.clearAuth()
  ElMessage.success('认证信息已清除')
  refreshLocalStorage()
}

const logout = async () => {
  await userStore.logout()
  ElMessage.success('已退出登录')
  refreshLocalStorage()
}

const testOrders = () => {
  console.log('测试订单页面跳转')
  router.push('/orders')
}

const testMessages = () => {
  console.log('测试消息页面跳转')
  router.push('/messages')
}

const testProfile = () => {
  console.log('测试个人中心跳转')
  router.push('/profile')
}

const testSettings = () => {
  console.log('测试设置页面跳转')
  router.push('/settings')
}

const refreshLocalStorage = () => {
  localStorageToken.value = localStorage.getItem('token')
  localStorageUserInfo.value = localStorage.getItem('userInfo')
}

const clearLocalStorage = () => {
  localStorage.clear()
  refreshLocalStorage()
  ElMessage.success('本地存储已清空')
}
</script>

<style scoped>
.test-page {
  padding: 2rem;
  max-width: 800px;
  margin: 0 auto;
}

.container {
  background: white;
  border-radius: 8px;
  padding: 2rem;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.status-section,
.action-section,
.test-section,
.debug-section,
.localStorage-section {
  margin-bottom: 2rem;
  padding: 1rem;
  border: 1px solid #eee;
  border-radius: 6px;
}

.action-buttons,
.test-buttons {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
}

pre {
  background: #f5f5f5;
  padding: 1rem;
  border-radius: 4px;
  overflow-x: auto;
  font-size: 0.9rem;
}
</style>
